/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.BaoCao;

import DTO.KhachHang_DTO;
import DTO.PhieuHuHai_DTO;
import Define.Define;
import GUI.ThuePhong.JIFDatPhong;
import GUI.Utilities;
import Server.QLKS_Interface;
import java.awt.HeadlessException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public final class JIFHoaDonThietHai extends javax.swing.JInternalFrame {
    DefaultComboBoxModel cbbModel_huhai;
    DefaultTableModel modelDs_huhai;
    ArrayList<KhachHang_DTO> ds_huhai;
    String ServerURL;
    QLKS_Interface QI;
    /**
     * Creates new form JIFHoaDonThietHai
     */
    public JIFHoaDonThietHai() {
        initComponents();
        this.setSize(880, 560);
        ServerURL = "qlks";
        try {
            QI = (QLKS_Interface) Naming.lookup(ServerURL);
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(JIFDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showCbbMaKhach_huhai();
        showDanhSach_huhai();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pannelHuHai = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        cbbMaKhach_huhai = new javax.swing.JComboBox();
        jLabel34 = new javax.swing.JLabel();
        jLabel35 = new javax.swing.JLabel();
        tfTenKhach_huhai = new javax.swing.JTextField();
        jLabel36 = new javax.swing.JLabel();
        tfSoDienThoai_huhai = new javax.swing.JTextField();
        jLabel37 = new javax.swing.JLabel();
        jLabel38 = new javax.swing.JLabel();
        tfTriGia = new javax.swing.JTextField();
        btLapPhieu = new javax.swing.JButton();
        jScrollPane9 = new javax.swing.JScrollPane();
        taNoiDungHuHai = new javax.swing.JTextArea();
        jScrollPane10 = new javax.swing.JScrollPane();
        tbDanhSach_huhai = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("LẬP HÓA ĐƠN THIỆT HẠI");

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin Khách hàng"));
        jPanel9.setToolTipText("");
        jPanel9.setName("Thông tin Khách hàng"); // NOI18N
        jPanel9.setPreferredSize(new java.awt.Dimension(237, 110));

        cbbMaKhach_huhai.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbMaKhach_huhaiItemStateChanged(evt);
            }
        });
        cbbMaKhach_huhai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbMaKhach_huhaiActionPerformed(evt);
            }
        });

        jLabel34.setText("Mã Khách");

        jLabel35.setText("Tên Khách Hàng");

        tfTenKhach_huhai.setEditable(false);

        jLabel36.setText("Số Điện Thoại");

        tfSoDienThoai_huhai.setEditable(false);

        jLabel37.setText("Nội dung hư hại: ");

        jLabel38.setText("Trị Giá:");

        btLapPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btLapPhieu.setText("Lập phiếu");
        btLapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieuActionPerformed(evt);
            }
        });

        taNoiDungHuHai.setColumns(20);
        taNoiDungHuHai.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        taNoiDungHuHai.setRows(5);
        jScrollPane9.setViewportView(taNoiDungHuHai);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel38)
                        .addGap(4, 4, 4)
                        .addComponent(tfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(jLabel35)
                                .addGap(18, 18, 18)
                                .addComponent(tfTenKhach_huhai, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(jLabel34)
                                .addGap(18, 18, 18)
                                .addComponent(cbbMaKhach_huhai, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(jLabel37)
                                .addGap(141, 141, 141)))
                        .addGap(52, 52, 52)
                        .addComponent(jLabel36)
                        .addGap(27, 27, 27)
                        .addComponent(tfSoDienThoai_huhai, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btLapPhieu)
                .addGap(62, 62, 62))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel34)
                            .addComponent(cbbMaKhach_huhai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTenKhach_huhai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel35)
                            .addComponent(jLabel36)))
                    .addComponent(tfSoDienThoai_huhai, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel37)))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel38)
                    .addComponent(tfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btLapPhieu)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane10.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbDanhSach_huhai.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDanhSach_huhai.setEnabled(false);
        jScrollPane10.setViewportView(tbDanhSach_huhai);

        javax.swing.GroupLayout pannelHuHaiLayout = new javax.swing.GroupLayout(pannelHuHai);
        pannelHuHai.setLayout(pannelHuHaiLayout);
        pannelHuHaiLayout.setHorizontalGroup(
            pannelHuHaiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pannelHuHaiLayout.createSequentialGroup()
                .addGroup(pannelHuHaiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pannelHuHaiLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 774, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pannelHuHaiLayout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        pannelHuHaiLayout.setVerticalGroup(
            pannelHuHaiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pannelHuHaiLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(134, 134, 134))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 831, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(pannelHuHai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 653, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 24, Short.MAX_VALUE)
                    .addComponent(pannelHuHai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 24, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbMaKhach_huhaiItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbMaKhach_huhaiItemStateChanged
        try {
            // TODO add your handling code here:
            //tfTenKhach_huhai.setText(KhachHang_BUS.layTenKHTheoMa(cbbMaKhach_huhai.getSelectedItem().toString()));
            tfTenKhach_huhai.setText(QI.LayTenKHTheoMa(cbbMaKhach_huhai.getSelectedItem().toString()));
            tfSoDienThoai_huhai.setText(QI.LaySoDTTheoMa(cbbMaKhach_huhai.getSelectedItem().toString()));
        } catch (RemoteException ex) {
            Logger.getLogger(JIFHoaDonThietHai.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cbbMaKhach_huhaiItemStateChanged

    private void cbbMaKhach_huhaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbMaKhach_huhaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbbMaKhach_huhaiActionPerformed

    private void btLapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieuActionPerformed
        // TODO add your handling code here:
        String maphieumax;
        String maphieu;
        try {
            //maphieumax = PhieuHuHai_BUS.laymaMax();
            maphieumax = QI.LayMaMax(Define.YC_LAYMAMAX_HUHAI);
            if (maphieumax.equals("")) {
                maphieu = Utilities.taoMaTuDong("MPHH000000", 4);
            } else {
                maphieu = Utilities.taoMaTuDong(maphieumax, 4);
            }
            String noidunghuHai = taNoiDungHuHai.getText();
            String triGia = tfTriGia.getText();
            try{
                Float tg = Float.parseFloat(triGia);
                if(tg<0||tg>100000000){
                    JOptionPane.showMessageDialog(rootPane, "Trị giá phải là số từ 0 đến 100.000.000!");
                    tfTriGia.setText("0");
                    return;
               }
            }catch(NumberFormatException | HeadlessException e){
                JOptionPane.showMessageDialog(rootPane, "Trị giá phải là số từ 0 đến 100.000.000!");
                return;
            }
            if (noidunghuHai.equals("") || triGia.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập nội dung hư hại và trị giá!");
                return;
            }
            String maKhach = cbbMaKhach_huhai.getSelectedItem().toString();
            PhieuHuHai_DTO p = new PhieuHuHai_DTO();
            p.setMaphieuhuHai(maphieu);
            p.setMaKhach(maKhach);
            p.setNoidunghuHai(noidunghuHai);
            p.setTriGia(triGia);

            //PhieuHuHai_BUS.Insert(p);
            QI.InsertHuHai(p);
            JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");

            showDanhSach_huhai();

        } catch (Exception ex) {
            Logger.getLogger(JIFHoaDonThietHai.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btLapPhieuActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLapPhieu;
    private javax.swing.JComboBox cbbMaKhach_huhai;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel37;
    private javax.swing.JLabel jLabel38;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JPanel pannelHuHai;
    private javax.swing.JTextArea taNoiDungHuHai;
    private javax.swing.JTable tbDanhSach_huhai;
    private javax.swing.JTextField tfSoDienThoai_huhai;
    private javax.swing.JTextField tfTenKhach_huhai;
    private javax.swing.JTextField tfTriGia;
    // End of variables declaration//GEN-END:variables
    protected void showCbbMaKhach_huhai() {
        cbbModel_huhai = (DefaultComboBoxModel) cbbMaKhach_huhai.getModel();
        try {
            //ds_huhai = KhachHang_BUS.layKhachDangThue();
            ds_huhai = QI.LayDSKhachDangThue();
        } catch (RemoteException e) {
        }

        for (KhachHang_DTO lp : ds_huhai) {
            cbbModel_huhai.addElement(lp.getMaKhachHang());
        }
    }

    protected void showDanhSach_huhai() {
        try {
            //modelDs_huhai = PhieuHuHai_BUS.layDanhSach();
            modelDs_huhai = QI.LoadTableModel(Define.YC_LAYDS_HUHAI);
            tbDanhSach_huhai.setModel(modelDs_huhai);
        } catch (RemoteException ex) {
            Logger.getLogger(JIFHoaDonThietHai.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
